package com.sunnada.nginx.service;

import com.sunnada.nginx.common.CMDUtil;
import com.sunnada.nginx.common.CommonFields;
import com.sunnada.nginx.conf.Configer;
import com.sunnada.nginx.exception.CustonException;
import com.sunnada.nginx.exception.NginxServiceManagerException;
import com.sunnada.nginx.thread.SystemProcessManager;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 与当前服务器中的ngin的运行状态相关的方法
 */
@Log
@Service("statusService")
public class StatusServiceImpl implements StatusService{
    @Autowired
    Configer configer;

    @Autowired
    SystemProcessManager systemProcessManager;

    /**
     * 服务是否启动
     * @Description :
     * @return : boolean
     * @Creation Date : 2018年2月1日 下午4:59:37
     * @Author : 郭胜凯
     */
    public boolean isStart() {
        String nginxName = CommonFields.NGINX;
        return systemProcessManager.isExist(nginxName);
    }

    /**
     * 停止服务
     * @Description :
     * @return : void
     * @Creation Date : 2018年2月2日 上午10:00:55
     * @Author : 郭胜凯
     */
    public void stop() throws CustonException {
        String excuse = CMDUtil.excuse(CommonFields.NGINX + " -s stop", configer.getNginxPath());
        if (!"".equals(excuse.trim())) {
            log.info("Nginx服务停止失败:" + excuse + ";尝试强制结束");
            SystemProcessManager.kill(CommonFields.NGINX);;
            log.info("Nginx服务已强制结束");
        }
    }

    /**
     * 启动服务
     * @Description :
     * @return : void
     * @Creation Date : 2018年2月2日 上午10:01:02
     * @Author : 郭胜凯
     */
    public void start() {
        if (isStart()) {
            throw new NginxServiceManagerException("Nginx启动时发现已有重复的服务启动。");
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                String excuse = null;
                try {
                    excuse = CMDUtil.excuse(CommonFields.NGINX + " -c " + configer.getNginxConfPath(), configer.getNginxPath());
                } catch (CustonException e) {
                    e.printStackTrace();
                }
                if (!"".equals(excuse.trim())) {
                    throw new NginxServiceManagerException("Nginx启动异常:" + excuse);
                }
            }
        }).start();
    }

    /**
     * 热加载
     * @Description :
     * @return : void
     * @Creation Date : 2018年2月2日 上午10:48:57
     * @Author : 郭胜凯
     */
    public void reload() throws CustonException {
        if (!isStart()) {
            throw new NginxServiceManagerException("Nginx未启动，请先启动Nginx。若您确定已在服务器上启动Nginx，那么请将本系统在sudo下执行。或手关闭Nginx并用该系统启动。");
        }
        checkConf();
        //重加载
        String excuse = CMDUtil.excuse(CommonFields.NGINX + " -s reload -c " + configer.getNginxConfPath(), configer.getNginxPath());
        if (!"".equals(excuse.trim()) && excuse.indexOf("error") != -1) {
            throw new NginxServiceManagerException("Nginx服务重加载配置失败:" + excuse);
        }
    }

    public void checkConf() throws CustonException{
        //校验
        String check = CMDUtil.excuse(CommonFields.NGINX + " -t -c " + configer.getNginxConfPath(), configer.getNginxPath());
        if (check.indexOf(CommonFields.NGINX  + ": configuration file " + configer.getNginxConfPath() + " test is successful") == -1) {
            throw new NginxServiceManagerException("Nginx配置文件校验失败:" + check);
        }
    }
}
